package com.ruoyi.web.controller.module.product;

import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.product.ProductAttrValue;
import com.ruoyi.system.domain.module.product.ProductOrderInfo;
import com.ruoyi.system.service.module.product.IProductAttrValueService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品属性值Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "商品属性值控制器", tags = {"商品属性值管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/product/attr/value")
public class ProductAttrValueController extends BaseController {

    private final IProductAttrValueService productAttrValueService;

    /**
     * 查询商品属性值列表
     */
    @ApiOperation("查询商品属性值列表")
    @GetMapping("/list")
    public TableDataInfo<ProductAttrValue> list(@Validated ProductAttrValue productAttrValue) {
		QueryWrapper<ProductAttrValue> qw = new QueryWrapper<>();
		if(productAttrValue.getProductId()!= null){
			qw.eq("product_id", productAttrValue.getProductId());
		}
		if(productAttrValue.getIsDel()!= null){
			qw.eq("is_del", productAttrValue.getIsDel());
		}
        return PageUtils.buildDataInfo(productAttrValueService.page(PageUtils.buildPage(),qw));
    }

    /**
     * 获取商品属性值详细信息
     */
    @ApiOperation("获取商品属性值详细信息")
    @GetMapping("/{id}")
    public AjaxResult<ProductAttrValue> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return AjaxResult.success(productAttrValueService.getById(id));
    }

    /**
     * 新增商品属性值
     */
    @ApiOperation("新增商品属性值")
    @Log(title = "商品属性值", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ProductAttrValue productAttrValue) {
        return toAjax(productAttrValueService.save(productAttrValue) ? 1 : 0);
    }

    /**
     * 修改商品属性值
     */
    @ApiOperation("修改商品属性值")
    @Log(title = "商品属性值", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ProductAttrValue productAttrValue) {
        return toAjax(productAttrValueService.updateById(productAttrValue) ? 1 : 0);
    }

    /**
     * 删除商品属性值
     */
    @ApiOperation("删除商品属性值")
    @Log(title = "商品属性值" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(productAttrValueService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
